﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using YinfangWeb.BLL;
using YinfangWeb.BLL.Enum;
using YinfangWeb.BLL.Model;
using YinfangWeb.Common;
using YinfangWeb.MLog;

namespace YinfangWeb.Api.Controllers
{
    public class UploadController : ApiController
    {
        #region 上传商品展示图
        /// <summary>
        /// 上传商品展示图
        /// </summary>
        /// <returns></returns>
        /// src\views\product\add\dlg_upload.vue
        public IHttpActionResult UploadSpDisplayMap()
        {
            try
            {
                string spId = HttpContext.Current.Request.Form["sp_id"];
                HttpFileCollection files = HttpContext.Current.Request.Files;
                HttpPostedFile file = files["image"];
                string fileName = file.FileName;
                byte[] buffer = new byte[file.ContentLength];
                using (BinaryReader reader = new BinaryReader(file.InputStream, Encoding.UTF8))
                {
                    buffer = reader.ReadBytes(file.ContentLength);
                }
                int SpID = Convert.ToInt32(spId);                
                var fileList = new FileServer().UploadSpDisplayMap(SpID,fileName, buffer,EnumProductFile_ViewLogo.Yes);//上传图片  
                if (!string.IsNullOrEmpty(fileList.NewFileName))
                {
                    new ProductBO().AddProductFile(SpID, fileList.NewFileName, EnumProductFile_FileType.Image, EnumProductFile_ViewLogo.Yes);//登记到商品附件里
                }
                return Json(new DataResult<FileNameObj>(fileList));
            }
            catch (Exception ex)
            {
                Logger.Default.Error("上传商品展示图出错！", ex);
                return Json(new Result(ResultCode.Busy, ex.Message));
            }
        }
        #endregion
        #region 上传商品富文本图片
        /// <summary>
        /// 上传商品富文本图片
        /// </summary>
        /// <returns></returns>
        /// src\views\product\add\dlg_upload.vue
        public IHttpActionResult UploadSpHtmlMap()
        {
            try
            {
                string spId = HttpContext.Current.Request.Form["sp_id"];
                HttpFileCollection files = HttpContext.Current.Request.Files;
                HttpPostedFile file = files["image"];
                string fileName = file.FileName;
                byte[] buffer = new byte[file.ContentLength];
                using (BinaryReader reader = new BinaryReader(file.InputStream, Encoding.UTF8))
                {
                    buffer = reader.ReadBytes(file.ContentLength);
                }
                int SpID = Convert.ToInt32(spId);
                var fileList = new FileServer().UploadSpDisplayMap(SpID, fileName, buffer, EnumProductFile_ViewLogo.No);//上传图片      
                if (!string.IsNullOrEmpty(fileList.NewFileName))
                {
                    new ProductBO().AddProductFile(SpID, fileList.NewFileName, EnumProductFile_FileType.Image, EnumProductFile_ViewLogo.Html);//登记到商品附件里
                }
                return Json(new DataResult<dynamic>(fileList));
            }
            catch (Exception ex)
            {
                Logger.Default.Error("上传商品富文本图片出错！", ex);
                return Json(new Result(ResultCode.Busy, ex.Message));
            }
        }
        #endregion
        #region 上传临时文件
        public IHttpActionResult UploadTemp()
        {
            try
            {
                //string fileName = HttpContext.Current.Request["name"];
                HttpFileCollection files = HttpContext.Current.Request.Files;
                HttpPostedFile file = files["image"];
                string fileName = file.FileName;
                byte[] buffer = new byte[file.ContentLength];
                using (BinaryReader reader = new BinaryReader(file.InputStream, Encoding.UTF8))
                {
                    buffer = reader.ReadBytes(file.ContentLength);
                }
                var url = new FileServer().UploadTemp(fileName, buffer);
                return Json(new DataResult<dynamic>(new { url }));
            }
            catch (Exception ex)
            {
                Logger.Default.Error("文件上传出错！", ex);
                return Json(new Result(ResultCode.Busy, ex.Message));
            }
        }
        #endregion
    }
}
